package org.jpass.dc.view.project;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.json.annotations.JSON;
import org.jpass.dc.business.project.bean.Clazz;
import org.jpass.dc.business.project.logic.ClassService;
import org.jpass.dc.business.project.logic.FileService;
import org.jpass.dc.business.project.logic.ProjectService;
import org.jpass.dc.business.user.bean.UserVOBean;
import org.jpass.dc.common.util.StringUtil;
import org.jpass.dc.view.common.BasicAction;

import com.opensymphony.xwork2.Action;

public class ClassAction extends BasicAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4836146166934529777L;
	
	Log log = LogFactory.getLog(ClassAction.class);
	
	private ProjectService projectService = new ProjectService();
	private FileService fileService = new FileService();
	private ClassService classService = new ClassService();
	
	private Clazz clazz = new Clazz();
	
	/* 返回给客户端，表示请求是否成功 */
	private boolean success;
	
	/* 发回给客户端的提示信息 */
	private String msg;
	
	/* 发回给客户端的提示信息的类型 */
	private String type;
	
	/* 文件的内容  */
	private String content;

	@JSON(serialize = false)
	public Clazz getClazz() {
		return clazz;
	}

	public void setClazz(Clazz clazz) {
		this.clazz = clazz;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String create() {
		success = validateData();
		try {
			success = validateData();
			if (success) {
				classService.create(clazz);
				content = classService.getContent();
			}
		} catch (IOException e) {
			success = false;
			type = "error";
			msg = "Server encountered an error.";
			log.error("新建class文件时出现异常，异常信息为：", e);
		}
		return Action.SUCCESS;
	}
	
	public boolean validateData() {
		UserVOBean user = this.getCurrentUser();
		if (StringUtil.isBlank(clazz.getProjectName()) || projectService.findByUserIdAndProjectName(user.getId(), clazz.getProjectName()) == null) {
			type = "warng";
			msg = "Please select a project.";
			return false;
		} else if (StringUtil.isBlank(clazz.getClassName())) {
			type = "warng";
			msg = "ClassName is empty.";
			return false;
		}
		
		String userWorkRootPath = this.getUserRealWrokRootPath();
		StringBuffer pathBuff = new StringBuffer()
			.append(userWorkRootPath)
			.append("/")
			.append(clazz.getProjectName())
			.append("/src");
		if (!StringUtil.isBlank(clazz.getPackageName())) {
			clazz.setParentPath(pathBuff.toString() + "/" + clazz.getPackageName().replace('.', '/'));
			pathBuff.append("/")
				.append(clazz.getPackageName().replace('.', '/'))
				.append("/")
				.append(clazz.getClassName())
				.append(".java");
		} else {
			clazz.setParentPath(pathBuff.toString());
			pathBuff.append("/")
				.append(clazz.getClassName())
				.append(".java");
		}
		
		if (fileService.isExist(pathBuff.toString())) {
			type = "warng";
			msg = "Class \'" + clazz.getClassName() +"\' is exist.";
			return false;
		}
		
		clazz.setPath(pathBuff.toString());
		
		return true;
	}
}
